#include "filewriter.h"

/**
 * 01.03.12 - Steven Hartin - Original file creation
 */

CFileWriter::CFileWriter(const string &szFilePath) :
    m_szFilePath(szFilePath)
{
    /**
     * Constructor, ensure that the file path is writeable.
     *
     * @param string szFilePath The path of the file to write
     */ 
}

CFileWriter::~CFileWriter()
{
    /**
     * Destructor, ensrue that any open file handle is closed.
     */
    _closeFile();
}

void CFileWriter::_openFile()
{
    /**
     * Opens the file stream and stores it in the file stream.
     *
     * Does no checking for overwriting files etc.
     */
    m_FileStream.open(m_szFilePath, std::ios::out | std::ios::binary);
}

void CFileWriter::_closeFile()
{
    /**
     * If the file is open, closes the file stream to ensure no file
     * handles are left open.
     */
    if (m_FileStream.is_open())
    {
        m_FileStream.close();
    }
}

void CFileWriter:: writeShort(short int nValue)
{
    /**
     * Writes a short to the file stream assuming that the stream is open.
     *
     * @param short int nValue The value to write to the stream
     */
    if (m_FileStream.is_open())
    {
        m_FileStream.write(reinterpret_cast<char*>(&nValue), 
                           sizeof(short int));
    }
}

void CFileWriter:: writeLong(long int nValue)
{
    /**
     * Writes a long to the file stream assuming that the stream is open.
     *
     * @param long int nValue The value to write to the stream
     */
    if (m_FileStream.is_open())
    {
        m_FileStream.write(reinterpret_cast<char*>(&nValue), 
                           sizeof(long int));
    }
}

void CFileWriter:: writeBool(bool bValue)
{
    /**
     * Writes a short to the file stream assuming that the stream is open.
     *
     * @param bool bValue The value to write to the stream
     */
    if (m_FileStream.is_open())
    {
        m_FileStream.write(reinterpret_cast<char*>(&bValue), 
                           sizeof(bool));
    }
}

void CFileWriter:: writeString(const string &szValue)
{
    /**
     * Writes a short to the file stream assuming that the stream is open.
     *
     * @param const string &szValue The value to write to the stream
     */
    if (m_FileStream.is_open())
    {
        char *szValueC = new char[szValue.length()];
        strcpy(szValueC, szValue.c_str());
        m_FileStream.write(szValueC, szValue.length());
        delete[] szValueC;
    }
}

void CFileWriter:: writeString(char *szValue)
{
    /**
     * Writes a short to the file stream assuming that the stream is open.
     *
     * @param char* szValue The value to write to the stream
     */
    if (m_FileStream.is_open())
    {
        m_FileStream.write(szValue, strlen(szValue) * sizeof(char));
    }
}

void CFileWriter:: writeChar(char chValue)
{
    /**
     * Writes a short to the file stream assuming that the stream is open.
     *
     * @param char chValue The value to write to the stream
     */
    if (m_FileStream.is_open())
    {
        m_FileStream.write(&chValue, 1);
    }
}

void CFileWriter:: writeFloat(float fValue)
{
    /**
     * Writes a short to the file stream assuming that the stream is open.
     *
     * @param float fValue The value to write to the stream
     */
    if (m_FileStream.is_open())
    {
        m_FileStream.write(reinterpret_cast<char*>(&fValue), sizeof(float));
    }
}

void CFileWriter:: writeDouble(double dValue)
{
    /**
     * Writes a short to the file stream assuming that the stream is open.
     *
     * @param double dValue The value to write to the stream
     */
    if (m_FileStream.is_open())
    {
        m_FileStream.write(reinterpret_cast<char*>(&dValue), sizeof(double));
    }
}